package com.oauth.google.server.picasa;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Required;

import com.oauth.common.server.OAuthGwtController;
import com.oauth.common.server.beans.OAuthAccessToken;
import com.oauth.common.server.beans.OAuthRequestToken;
import com.oauth.common.server.util.OAuthRequestUtil;
import com.oauth.components.client.beans.OAuthUrl;
import com.oauth.google.client.beans.orkut.HttpQueryString;
import com.oauth.google.client.beans.picassa.GooglePicasaAlbum;
import com.oauth.google.client.beans.picassa.GooglePicasaModel;
import com.oauth.google.client.beans.picassa.PicasaPhoto;
import com.oauth.google.client.exception.GoogleOAuthException;
import com.oauth.google.client.rpc.picasa.PicasaRemoteService;
import com.oauth.google.server.beans.GoogleRequestToken;
import com.oauth.google.server.constants.GoogleConstants;
import com.oauth.google.server.picasa.service.PicasaOAuthService;
import com.oauth.service.generic.beans.OAuthServiceBean;

public class GooglePicasaController extends OAuthGwtController implements
		PicasaRemoteService {

	private static final long serialVersionUID = -5980008747750684114L;
	private PicasaOAuthService mPicasaOAuthService;
	private Logger logger = Logger.getLogger(this.getClass());

	/**
	 * @param pPicasaOAuthService
	 *            the picasaOAuthService to set
	 */
	@Required
	public void setPicasaOAuthService(PicasaOAuthService pPicasaOAuthService) {
		mPicasaOAuthService = pPicasaOAuthService;
	}

	@Override
	public OAuthUrl getSignInUrl() throws GoogleOAuthException {
		GoogleRequestToken requestToken;
		try {
			requestToken = mPicasaOAuthService.getRequestToken();
		} catch (Exception e) {
			logger.error("Error in getting request token", e);
			throw new GoogleOAuthException();
		}
		OAuthRequestUtil.createTokenCookies(getHttpResponse(), requestToken,
				GoogleConstants.PICASA_TOKEN_COOKIE,
				GoogleConstants.PICASA_TOKEN_SECRETCOOKIE);
		OAuthUrl oauthUrl = new OAuthUrl();
		oauthUrl.setOAuthUrl(requestToken.getAuthorizationUrl());
		return oauthUrl;
	}

	@Override
	public GooglePicasaModel getPicasaAlbums(HttpQueryString pQueryString)
			throws GoogleOAuthException {
		GooglePicasaModel model = new GooglePicasaModel();
		Long memberId = OAuthRequestUtil.getMemberId(getHttpRequest());
		try {
			OAuthAccessToken accessToken = mPicasaOAuthService
					.getAccessTokenFromDB(memberId);
			if (accessToken == null) {
				/* Get request token from cookie. */
				OAuthRequestToken requestToken = OAuthRequestUtil
						.getOAuthTokenFromCookie(getHttpRequest(),
								GoogleConstants.PICASA_TOKEN_COOKIE,
								GoogleConstants.PICASA_TOKEN_SECRETCOOKIE);
				if (requestToken == null) {
					model.setLoginView(true);
					return model;// Go to login page, as there is no access
					// token in
					// db and no cookie.
				} else {
					accessToken = mPicasaOAuthService
							.getAccessTokenFromRequestToken(requestToken,
									pQueryString.getQueryString());

					/* Store access token for future use */
					mPicasaOAuthService.persistAccessToken(accessToken,
							memberId);
					deleteGoogleCookies();
				}
			}
			// finally you got the access token which is stored in db.
			List<GooglePicasaAlbum> albums = mPicasaOAuthService
					.getPicasaAlbums(memberId);
			model.setAlbums(albums);
			return model;
		} catch (Exception e) {
			deleteGoogleCookies();
			logger.error("Error in fetching picasa albums", e);
			throw new GoogleOAuthException();
		}
	}

	private void deleteGoogleCookies() {
		OAuthRequestUtil.deleteTokenCookies(getHttpResponse(),
				GoogleConstants.PICASA_TOKEN_COOKIE,
				GoogleConstants.PICASA_TOKEN_SECRETCOOKIE);
	}

	@Override
	public List<PicasaPhoto> getPhotosForAlbum(String pAlbumId)
			throws Exception {
		Long memberId = OAuthRequestUtil.getMemberId(getHttpRequest());
		List<PicasaPhoto> photosForAlbum = mPicasaOAuthService
				.getPhotosForAlbum(memberId, pAlbumId);
		return photosForAlbum;
	}

	@Override
	protected <T extends OAuthServiceBean> Class<T> getServiceBeanClass() {
		// TODO Auto-generated method stub
		return null;
	}

}
